extends Node2D

@export var star_count = 10
@export var change_interval = 1.0 # 改变位置的时间间隔

var positions = []
var timer = 0.0
var particles = []

func _ready():
	# 生成初始位置
	_generate_random_positions()
	_create_particles()
	set_process(true)

func _process(delta):
	timer += delta
	if timer >= change_interval:
		timer = 0.0
		_update_random_position()

func _generate_random_positions():
	positions.clear()
	for i in range(star_count):
		var random_pos = Vector2(randf() * get_viewport().size.x, randf() * get_viewport().size.y)
		positions.append(random_pos)

func _create_particles():
	# 创建并配置 CPUParticles2D 节点
	for i in range(star_count):
		var particle = CPUParticles2D.new()
		particle.position = positions[i]
		add_child(particle)
		particles.append(particle)


func _update_random_position():
	# 随机选择一颗星星改变位置
	var index = randi() % star_count
	positions[index] = Vector2(randf() * get_viewport().size.x, randf() * get_viewport().size.y)
	_apply_positions()

func _apply_positions():
	# 更新粒子的位置
	for i in range(star_count):
		particles[i].position = positions[i]
